import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws Exception {
        String input = "D:\\pdf-for-test\\简单文字型.pdf";
        String outputFile = Paths.get(FilenameUtils.getFullPath(input), FilenameUtils.getBaseName(input) + ".txt").toString();

        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        long st =  System.currentTimeMillis();
        String content = extract(input);
        System.out.println("Time took:" + (System.currentTimeMillis()  - st) + "ms" );
        saveContentToFile(content, outputFile);

        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println("Done");
    }


    /**
     * 提取PDF内容
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String extract(String file) throws Exception {
        PDFExtract extractor = new PDFExtract();
        String content = extractor.extract(file);
        return content;
    }


    /**
     * 保存内容到文件
     *
     * @param content
     * @param file
     * @throws IOException
     */
    public static void saveContentToFile(String content, String file) throws IOException {
        File outFile = new File(file);
        // 创建目录
        outFile.getParentFile().mkdirs();
        FileUtils.write(outFile, content, Charset.forName("UTF-8"));
    }
}
